import { Injectable } from '@nestjs/common';
import * as nodemailer from 'nodemailer';

const transporter = nodemailer.createTransport({
  host: 'smtp.163.com',
  port: 465,
  secure: true,
  auth: {
    user: '18250035075@163.com',
    pass: 'SOBJKGDJDGHVGYJV',
  },
});

@Injectable()
export class SendEmailService {
  constructor() {}

  /**
   * 随机获取6为纯数字验证码
   * @returns 6为纯数字验证码
   */
  generateRandomCode = (): string => {
    const code: string = Math.floor(Math.random() * 1000000)
      .toString()
      .padStart(6, '0');
    return code;
  };

  /**
   * 发送邮箱验证码(调用api)
   * @param {} captcha 验证码
   * @returns 返回结果
   */
  sendCaptchaToEmail(targetEmail: string, emailTitle: string, content: string) {
    const receiver = {
      from: `"涉外企业合规智能服务平台"<18250035075@163.com>`,
      // 主题
      subject: emailTitle,
      // 收件人 的邮箱 可以是其他邮箱 不一定是qq邮箱
      to: targetEmail,
      html: content,
    };
    return transporter.sendMail(receiver);
  }
  // sendCaptchaToEmail(targetEmail: string, captcha: string, emailTitle: string) {
  //   const messageTemplate = `本次注册的验证码为${captcha}，有效时间为15分钟！`;
  //   return axios({
  //     url: 'https://tongyue.qxamoy.com:8996/send_email',
  //     method: 'POST',
  //     data: {
  //       to_email: targetEmail,
  //       subject: emailTitle,
  //       message: messageTemplate,
  //     },
  //   });
  // }
}
